package org.example.gui.panel;

import org.example.dao.BudgetDAO;
import org.example.gui.listener.ConfigListener;
import org.example.gui.model.BudgetComboBoxModel;
import org.example.util.ColorUtil;
import org.example.util.DateUtil;
import org.example.util.GUIUtil;

import javax.swing.*;
import java.awt.*;

/**
 * 界面类 ConfigPanel 设置页
 */

public class ConfigPanel extends WorkingPanel {
    public static ConfigPanel instance = new ConfigPanel();
    public BudgetComboBoxModel model = new BudgetComboBoxModel();
    public JComboBox<String> cbBudgetPerMonth = new JComboBox<>(model);
    public JTextField tfBudget = new JTextField();
    public JButton bTruncate = new JButton("重置数据库");
    public JButton bSubmit = new JButton("更新");


    public ConfigPanel() {
        JLabel lMsg = new JLabel("设置每月预算(￥)");
        GUIUtil.setColor(ColorUtil.grayColor, lMsg);
        GUIUtil.setColor(ColorUtil.blueColor, bSubmit);

        JPanel pNorth = new JPanel();
        JPanel pSouth = new JPanel();
        int gap = 40;
        pNorth.setLayout(new GridLayout(4, 1, gap, gap));
        pNorth.add(lMsg);
        pNorth.add(cbBudgetPerMonth);
        pNorth.add(tfBudget);
        pNorth.add(bSubmit);
        pSouth.add(bTruncate);
        this.setLayout(new BorderLayout());
        this.add(pNorth, BorderLayout.NORTH);
        this.add(pSouth, BorderLayout.SOUTH);

        this.addListener();
    }


    @Override
    public void addListener() {
        ConfigListener l = new ConfigListener();
        cbBudgetPerMonth.addItemListener(e -> {
            String budget = BudgetDAO.getValueByKey(BudgetDAO.budget + (cbBudgetPerMonth.getSelectedIndex() + 1));
            tfBudget.setText(budget);
        });
        bSubmit.addActionListener(l);
        bTruncate.addActionListener(l);
    }

    @Override
    public void updateData() {
//        tfBudget.setText(BudgetDAO.getValueByKey(BudgetDAO.budget));
        cbBudgetPerMonth.setSelectedIndex(DateUtil.thisMonth());
        tfBudget.grabFocus();
    }
}